$(function() {
    $.initPage(eContractDetail.load.start);
});

var eContractDetail = new BaseFrame();

eContractDetail.addBlock(
    'basicInformation',
    'vendorInformation',
    'descriptionInformation',
    'attachmentInformation',
    'approverInformation',
    'approvelInformation',
    'operationInformation'
);

//load module
eContractDetail.load.addStart(function() {
    $.getFormDetail()
        .then(function(data) {
            _data = $.extend({}, data);
            $('#form-detail').data('detail', $.extend({}, _data.formDetail ? _data.formDetail.formDetail : {}));
            return $.pageStart(_eLancoSettings);
        })
        .then(function() {
            if ($.isEmpty(_data.formDetail)) {
                $.alert(_enumMessage.noRecord, $.exitForm);
            } else {
                eContractDetail.event.runTask();
                eContractDetail.load.runTask();
                eContractDetail.verify.status();
            };
        });
});

eContractDetail.load.addBind('createTitle', function() {
    var currentPage = $.getCurrentPage(_eLancoSettings.pageList);
    $('title').text(currentPage.title);
});

eContractDetail.load.addBind('createNav', function() {
    var navList = $.getUserAuthorityList(_user.roleList, _eLancoSettings.authorityList, _eLancoSettings.pageList);
    $('#logout').on('click', $.logoff);
    $('#block-nav').createNavList(navList).removeClass('dn');
});

eContractDetail.load.basicInformation.addBind('createBasicInformation', function() {
    var domStorage = $('#form-detail');
    domStorage.data().detail.legalEntity = '礼来（上海）动物保健有限公司';
    $('#field-detail-legalEntity').createString(domStorage, 'detail.legalEntity', {
        required: true,
        readonly: true,
    });
    $('#field-detail-department').createSelect(domStorage, 'detail.department', $.getKeyValueListByIdName(_data.departmentList), {
        required: true,
    });
    $('#field-detail-isMeeting').createSingleCheckbox(domStorage, 'detail.isMeeting');
    $('#field-detail-activityNameDescription').createString(domStorage, 'detail.activityNameDescription', {
        required: true,
    });
    $('#field-detail-totalContractAmount').createDecimal(domStorage, 'detail.totalContractAmount', {
        required: true,
        readonly: true,
    });
    $('#field-detail-projectNumber').createString(domStorage, 'detail.projectNumber', {
        required: true,
    });
    $('#field-detail-activityDate').createDoubleDatepicker(domStorage, 'detail.activityDateStart, detail.activityDateEnd', {
        required: true,
    });
    var costCenterAndAmountList = domStorage.data('detail').costCenterAndAmountList;
    if ($.isEmpty(costCenterAndAmountList))
        eContractDetail.load.basicInformation.createCostCenterAndAmountItem();
    else
        for (var i = 0; i < costCenterAndAmountList.length; i++)
            eContractDetail.load.basicInformation.createCostCenterAndAmountItem(costCenterAndAmountList[i]);
    $('#field-detail-costCenterAndAmountList .btn:eq(0)').text('新建').removeAttr('delete').attr('create', '');
    $('#field-detail-createPo').createSingleCheckbox(domStorage, 'detail.createPo');
    $('#field-detail-acdd').createSingleCheckbox(domStorage, 'detail.acdd');
    $('#block-basic-information').removeClass('dn');
});

eContractDetail.load.basicInformation.addEvent('createCostCenterAndAmountItem', function(itemData) {
    itemData = itemData || {};
    var template = $('#template-costCenterAndAmountList-item').html();
    $('#field-detail-costCenterAndAmountList tbody').createTemplate(template);
    var itemDomStorage = $('#field-detail-costCenterAndAmountList tbody tr:last');
    itemDomStorage.data('item', $.extend({}, itemData));
    $('#field-detail-costCenterAndAmountList tbody tr:last td:eq(0)').createSelect(itemDomStorage, 'item.costCenter', $.createDropdownList('costCenter'), {
        required: true,
    });
    $('#field-detail-costCenterAndAmountList tbody tr:last td:eq(1)').createDecimal(itemDomStorage, 'item.amount', {
        required: true,
    });
    $('#field-detail-costCenterAndAmountList tbody tr:last td:eq(1)').createUnit('￥');
});

eContractDetail.load.vendorInformation.addBind('createVendorInformation', function() {
    var domStorage = $('#form-detail');
    $('#field-detail-vendorName').createString(domStorage, 'detail.vendorName', {
        required: true,
    });
    $('#field-detail-isVendor').createSingleCheckbox(domStorage, 'detail.isVendor');
    $('#block-vendor-information').removeClass('dn');
});

eContractDetail.load.descriptionInformation.addBind('createDescriptionInformation', function() {
    var domStorage = $('#form-detail');
    $('#field-detail-briefDescription').createTextarea(domStorage, 'detail.briefDescription', {
        required: true,
    });
    $('#block-description-information').removeClass('dn');
});

eContractDetail.load.attachmentInformation.addBind('createAttachmentInformation', function() {
    var domStorage = $('#form-detail');
    $('#field-detail-projectContractAttachmentList').createAttachment(domStorage, 'detail.projectContractAttachmentList');
    $('#field-detail-vendorRequestFormAttachmentList').createAttachment(domStorage, 'detail.vendorRequestFormAttachmentList');
    $('#block-attachment-information').removeClass('dn');
});

eContractDetail.load.approverInformation.addBind('createApproverInformation', function() {
    var domStorage = $('#form-detail');
    $('#field-detail-buyApprover').createSelect(domStorage, 'detail.approvalStep1Approver', $.createApprovalList(1), {
        required: true,
    });
    $('#field-detail-legalApprover').createSelect(domStorage, 'detail.approvalStep2Approver', $.createApprovalList(2), {
        required: true,
    });
    $('#field-detail-ecApprover').createSelect(domStorage, 'detail.approvalStep3Approver', $.createApprovalList(3), {
        required: true,
    });
    $('#field-detail-atsApprover').createSelect(domStorage, 'detail.approvalStep4Approver', $.createApprovalList(4), {
        required: true,
    });
    $('#field-detail-stampApprover').createSelect(domStorage, 'detail.approvalStep5Approver', $.createApprovalList(5), {
        required: true,
        disabled: true,
    });
    $('#field-detail-createPo').findFormUnit().trigger('change').verifyClean();
    $('#block-approver-information').removeClass('dn');
});

eContractDetail.load.approverInformation.addBind('createApprovelInformation', function() {
    var domStorage = $('#form-detail');
    var data = _data.formDetail;
    var approvalStep = data.approvalStep;
    var approvalList = data.approvalList;
    if ($.isEmpty(approvalList)) return false;
    var template = $('#template-approval-item').html();
    var curtemplate = $('#template-curapproval-item').html();
    for (var i = 0; i < approvalList.length; i++) {
        var approverItem = approvalList[i];
        if (approverItem.approvalStep <= approvalStep &&
            approverItem.approvalStatus != 0) {
            $('#approval-information').createTemplate(template, approverItem);
        }
        if (approverItem.approverEmployeeId == _user.employeeId &&
            approverItem.approvalStep == approvalStep &&
            approverItem.approvalStatus == _enumApprovalStatus.待审批 &&
            data.formStatus >= _enumFormStatus.submitted) {
            domStorage.data('detail').comment = '';
            $('#curapproval-information').createTemplate(curtemplate, approverItem).find('#curapproval-common').createTextarea(domStorage, 'detail.comment', {
                verify: false,
                locked: true,
            });
        }
    }
    if (data.approvalFinalStep == data.approvalStep) {
        $('#curapproval-information').find('.curapproval-common').addClass('dn');
    }
    if ($('#block-approval-information .approval-information').length != 0) {
        $('#block-approval-information').removeClass('dn');
    }
    if ($('#block-curapproval-information .curapproval-information').length != 0) {
        $('#block-curapproval-information').removeClass('dn');
    }
});

eContractDetail.load.operationInformation.addBind('createOperationInformation', function() {
    $('#block-operation-information').removeClass('dn');
});

//event module

eContractDetail.event.basicInformation.addBind('approverChange', function() {
    $('#field-detail-isMeeting, #field-detail-totalContractAmount, #field-detail-createPo, #field-detail-acdd').on('changeCallback', eContractDetail.event.basicInformation.approverChangeCallback);
});

eContractDetail.event.basicInformation.addEvent('approverChangeCallback', function() {
    var formDetail = $('#form-detail').data('detail');
    var totalContractAmount = formDetail.totalContractAmount;
    if (eContractDetail.verify.haveBuyApprover()) {
        $('#field-detail-buyApprover').parent().removeClass('dn');
    } else {
        $('#field-detail-buyApprover').findFormUnit().val(null).trigger('change').verifyClean().parents('tr').addClass('dn');
    }
    if (eContractDetail.verify.haveLegalApprover()) {
        $('#field-detail-legalApprover').parent().removeClass('dn');
    } else {
        $('#field-detail-legalApprover').findFormUnit().val(null).trigger('change').verifyClean().parents('tr').addClass('dn');
    }
    if (eContractDetail.verify.haveEcApprover()) {
        $('#field-detail-ecApprover').parent().removeClass('dn');
    } else {
        $('#field-detail-ecApprover').findFormUnit().val(null).trigger('change').verifyClean().parents('tr').addClass('dn');
    }
    if (eContractDetail.verify.haveAtsApprover()) {
        var managerEmployeeIdList = _data.roleList[6].others.managerIds;
        var oldOptionList = $.createApprovalList(4);
        var newOptionList = oldOptionList;
        var atsApproverDom = $('#field-detail-atsApprover');
        if (totalContractAmount >= 340000) {
            newOptionList = [];
            for (var a = 0; a < oldOptionList.length; a++) {
                var oldOptionItem = oldOptionList[a];
                for (var b = 0; b < managerEmployeeIdList.length; b++) {
                    var managerEmployeeIdItem = managerEmployeeIdList[b];
                    if (oldOptionItem.key == managerEmployeeIdItem) newOptionList.push(oldOptionItem);
                }
            }
        }
        if (atsApproverDom.findFormUnit().length == 0) {
            atsApproverDom.on('createCallback', function() {
                atsApproverDom.reloadOptionList(newOptionList).reloadSelectedOption();
            });
        } else {
            atsApproverDom.reloadOptionList(newOptionList).reloadSelectedOption();
        }
        $('#field-detail-atsApprover').parent().removeClass('dn');
    } else {
        $('#field-detail-atsApprover').findFormUnit().val(null).trigger('change').verifyClean().parents('tr').addClass('dn');
    }
});

eContractDetail.event.basicInformation.addBind('costCenterAndAmountListOperation', function() {
    $('#field-detail-costCenterAndAmountList').on('click', '.btn[create]', function() {
        if ($('#field-detail-costCenterAndAmountList tr[data]').length >= 10) return $.alert(eContractDetail.message.basicInformation.costCenterAndAmountListLimit);
        eContractDetail.load.basicInformation.createCostCenterAndAmountItem();
    });

    $('#field-detail-costCenterAndAmountList').on('click', '.btn[delete]', function() {
        $(this).parents('tr[data]').remove();
        $('#field-detail-costCenterAndAmountList .form-control:text').trigger('change');
    });

    $('#field-detail-costCenterAndAmountList').on('change', '.form-control:text', function() {
        var thisDom = $(this);
        var totalContractAmountDom = $('#field-detail-totalContractAmount');
        var totalContractAmount = 0;
        var amountDomList = $('#field-detail-costCenterAndAmountList .form-control:text');
        for (var i = 0; i < amountDomList.length; i++)
            totalContractAmount = $.mathJia(totalContractAmount, $.accountToNumber(amountDomList.eq(i).val()));
        totalContractAmountDom.findFormUnit().val(totalContractAmount).trigger('change').verifyClean();
        if ($.accountToNumber(totalContractAmount) >= 100000) $('#field-detail-vendorRequestFormAttachmentList').parent().removeClass('dn');
        else $('#field-detail-vendorRequestFormAttachmentList').parent().addClass('dn');
    });
});

eContractDetail.event.vendorInformation.addBind('isVendorChange', function() {
    // $('#field-detail-isVendor').on('changeCallback', function (event, isChecked) {
    //   if (isChecked) $('#field-detail-vendorRequestFormAttachmentList').parent().removeClass('dn');
    //   else $('#field-detail-vendorRequestFormAttachmentList').parent().addClass('dn');
    // });
});

eContractDetail.event.operationInformation.addBind('operation', function() {
    $('#block-operation-information').on('click', '.btn[save]', eContractDetail.form.saveForm);

    $('#block-operation-information').on('click', '.btn[rejectsave]', eContractDetail.form.rejectSaveForm);

    $('#block-operation-information').on('click', '.btn[submit]', function() {
        if (!eContractDetail.verify.submitForm()) return;
        eContractDetail.form.submitForm();
    });

    $('#block-operation-information').on('click', '.btn[approve]', eContractDetail.form.approveForm);

    $('#block-operation-information').on('click', '.btn[reject]', eContractDetail.form.rejectForm);

    $('#block-operation-information').on('click', '.btn[exit]', eContractDetail.form.exitForm);
});

//form module
eContractDetail.form.addEvent('saveForm', function() {
    var id = $.isEmpty($.getParam('id')) ? 0 : $.getParam('id');
    var formId = $.isEmpty($.getParam('formId')) ? 0 : $.getParam('formId');
    var formDetail = eContractDetail.save.formDetail();
    var params = $.getFormDetailParams(_data.formDetail, {
        formId: formId,
        formDetail: formDetail
    });
    $.post(_apiPath + '/eLancos/FormDetails/' + id + '/Save', params)
        .then(function(data) {
            $.alert(_enumMessage.saveSuccess, eContractDetail.form.exitForm);
        });
});

eContractDetail.form.addEvent('rejectSaveForm', function() {
    var id = $.isEmpty($.getParam('id')) ? 0 : $.getParam('id');
    var formId = $.isEmpty($.getParam('formId')) ? 0 : $.getParam('formId');
    var formDetail = eContractDetail.save.formDetail();
    var params = $.getFormDetailParams(_data.formDetail, {
        formId: formId,
        formDetail: formDetail
    });
    $.post(_apiPath + '/eLancos/FormDetails/' + id + '/Save', params)
        .then(function(data) {
            return $.post(_apiPath + '/ELancos/FormDetails/' + id + '/Cancel', params);
        }).then(function(data) {
            $.alert(_enumMessage.rejectSaveSuccess, manpowerRequisitionDetail.form.exitForm);
        });
});

eContractDetail.form.addEvent('submitForm', function() {
    var id = $.isEmpty($.getParam('id')) ? 0 : $.getParam('id');
    var formId = $.isEmpty($.getParam('formId')) ? 0 : $.getParam('formId');
    var formDetail = eContractDetail.save.formDetail();
    formDetail.totalContractAmount = $.accountToNumber(formDetail.totalContractAmount);
    var params = $.getFormDetailParams(_data.formDetail, {
        formId: formId,
        formDetail: formDetail
    });
    $.post(_apiPath + '/eLancos/FormDetails/' + id + '/Submit', params)
        .then(function(data) {
            $.alert(_enumMessage.submitSuccess, eContractDetail.form.exitForm);
        });
});

eContractDetail.form.addEvent('approveForm', function() {
    var id = $.isEmpty($.getParam('id')) ? 0 : $.getParam('id');
    var formId = $.isEmpty($.getParam('formId')) ? 0 : $.getParam('formId');
    var formDetail = eContractDetail.save.formDetail();
    var params = $.getFormDetailParams(_data.formDetail, {
        formId: formId,
        formDetail: formDetail
    });
    $.post(_apiPath + '/eLancos/FormDetails/' + id + '/Approval', params)
        .then(function(data) {
            $.alert(_enumMessage.approveSuccess, eContractDetail.form.exitForm);
        });
});

eContractDetail.form.addEvent('rejectForm', function() {
    var formDetail = eContractDetail.save.formDetail();
    if ($.isEmpty(formDetail.comment)) {
        $.alert(_enumMessage.refuseCommentIsEmpty);
        return;
    }
    var id = $.isEmpty($.getParam('id')) ? 0 : $.getParam('id');
    var formId = $.isEmpty($.getParam('formId')) ? 0 : $.getParam('formId');
    var formDetail = eContractDetail.save.formDetail();
    var params = $.getFormDetailParams(_data.formDetail, {
        formId: formId,
        formDetail: formDetail
    });
    $.post(_apiPath + '/eLancos/FormDetails/' + id + '/Reject', params)
        .then(function(data) {
            $.alert(_enumMessage.rejectSuccess, eContractDetail.form.exitForm);
        });
});

eContractDetail.form.addEvent('exitForm', $.exitForm);

//save module
eContractDetail.save.addEvent('formDetail', function() {
    var formDetail = $.extend({}, $('#form-detail').data('detail'));
    formDetail.costCenterAndAmountList = eContractDetail.save.basicInformation.costCenterAndAmountList();
    if (formDetail.totalContractAmount < 100000) formDetail.vendorRequestFormAttachmentList = [];
    return formDetail;
});

eContractDetail.save.basicInformation.addEvent('costCenterAndAmountList', function() {
    var data = $.extend({}, $('#form-detail').data('detail'));
    var costCenterAndAmountList = [];
    var costCenterAndAmountListDom = $('#field-detail-costCenterAndAmountList tr[data]');
    for (var i = 0; i < costCenterAndAmountListDom.length; i++)
        costCenterAndAmountList.push(costCenterAndAmountListDom.eq(i).data('item'));
    return costCenterAndAmountList;
});

//verify module
eContractDetail.verify.addEvent('submitForm', function() {
    var judge = true;
    var data = $('#form-detail').data('detail');
    $('#form-detail *[form]').findFormUnit().verifyClean();
    _message = '\n' + _enumMessage.operationError;
    if ($.isEmpty(data.legalEntity)) {
        judge = false;
        $('#field-detail-legalEntity').verifyErrorAndHelp(eContractDetail.message.basicInformation.legalEntityRequired);
    }
    if ($.isEmpty(data.department)) {
        judge = false;
        $('#field-detail-department').verifyErrorAndHelp(eContractDetail.message.basicInformation.departmentRequired);
    }
    // if ($.isEmpty(data.isMeeting)) {
    //   judge = false;
    //   $('#field-detail-isMeeting').verifyErrorAndHelp(eContractDetail.message.basicInformation.isMeetingRequired);
    // }
    if ($.isEmpty(data.activityNameDescription)) {
        judge = false;
        $('#field-detail-activityNameDescription').verifyErrorAndHelp(eContractDetail.message.basicInformation.activityNameDescriptionRequired);
    }
    if ($.isEmpty(data.totalContractAmount)) {
        judge = false;
        $('#field-detail-totalContractAmount').verifyErrorAndHelp(eContractDetail.message.basicInformation.totalContractAmountRequired);
    } else {
        if (!$.isDecimal(data.totalContractAmount)) {
            judge = false;
            $('#field-detail-totalContractAmount').verifyErrorAndHelp(eContractDetail.message.basicInformation.totalContractAmountInvalid);
        }
    }
    if ($.isEmpty(data.activityDateStart)) {
        judge = false;
        $('#field-detail-activityDate td[start]').verifyErrorAndHelp(eContractDetail.message.basicInformation.activityDateStartRequired);
    }
    if ($.isEmpty(data.activityDateEnd)) {
        judge = false;
        $('#field-detail-activityDate td[end]').verifyErrorAndHelp(eContractDetail.message.basicInformation.activityDateEndRequired);
    }
    if (!eContractDetail.verify.basicInformation.costCenterAndAmountList()) judge = false;
    // if ($.isEmpty(data.createPo)) {
    //   judge = false;
    //   $('#field-detail-createPo').verifyErrorAndHelp(eContractDetail.message.basicInformation.createPoRequired);
    // }
    // if ($.isEmpty(data.acdd)) {
    //   judge = false;
    //   $('#field-detail-acdd').verifyErrorAndHelp(eContractDetail.message.basicInformation.acddRequired);
    // }
    if ($.isEmpty(data.vendorName)) {
        judge = false;
        $('#field-detail-vendorName').verifyErrorAndHelp(eContractDetail.message.vendorInformation.vendorNameRequired);
    }
    if (eContractDetail.verify.haveBuyApprover() && $.isEmpty(data.approvalStep1Approver)) {
        judge = false;
        $('#field-detail-buyApprover').verifyErrorAndHelp(eContractDetail.message.approverInformation.buyApproverRequired);
    }
    if (eContractDetail.verify.haveLegalApprover() && $.isEmpty(data.approvalStep2Approver)) {
        judge = false;
        $('#field-detail-legalApprover').verifyErrorAndHelp(eContractDetail.message.approverInformation.legalApproverRequired);
    }
    if (eContractDetail.verify.haveEcApprover() && $.isEmpty(data.approvalStep3Approver)) {
        judge = false;
        $('#field-detail-ecApprover').verifyErrorAndHelp(eContractDetail.message.approverInformation.ECApproverRequired);
    }
    if (eContractDetail.verify.haveAtsApprover() && $.isEmpty(data.approvalStep4Approver)) {
        judge = false;
        $('#field-detail-atsApprover').verifyErrorAndHelp(eContractDetail.message.approverInformation.atsApproverRequired);
    }
    if ($.isEmpty(data.approvalStep5Approver)) {
        judge = false;
        $('#approvalStep3Approver').verifyErrorAndHelp(eContractDetail.message.approverInformation.stampApproverRequired);
    }
    if ($.isEmpty(data.projectContractAttachmentList)) {
        judge = false;
        $('#field-detail-projectContractAttachmentList').verifyErrorAndHelp(eContractDetail.message.attachmentInformation.projectContractAttachmentListRequired);
    }
    if (data.totalContractAmount >= 100000) {
        if ($.isEmpty(data.vendorRequestFormAttachmentList)) {
            judge = false;
            $('#field-detail-vendorRequestFormAttachmentList').verifyErrorAndHelp(eContractDetail.message.attachmentInformation.vendorRequestFormAttachmentListRequired);
        }
    }
    if (!eContractDetail.verify.attachmentInformation.attachmentSizeLimit()) judge = false
    if (!eContractDetail.verify.createPo()) judge = false;
    if (!judge) $.alert(_message);
    return judge;
});

eContractDetail.verify.basicInformation.addEvent('costCenterAndAmountList', function() {
    var judge = true;
    var costCenterAndAmountListDom = $('#field-detail-costCenterAndAmountList tr[data]');
    for (var i = 0; i < costCenterAndAmountListDom.length; i++) {
        var costCenterAndAmountItemDom = costCenterAndAmountListDom.eq(i);
        var costCenterDom = costCenterAndAmountItemDom.find('.form-control').eq(0);
        var amountDom = costCenterAndAmountItemDom.find('.form-control').eq(1);
        var data = costCenterAndAmountItemDom.data('item');
        if ($.isEmpty(data.costCenter)) {
            judge = false;
            costCenterDom.verifyError().verifyHelp(eContractDetail.message.basicInformation.costCenterRequired);
        }
        if ($.isEmpty(data.amount)) {
            judge = false;
            amountDom.verifyError().verifyHelp(eContractDetail.message.basicInformation.amountRequired);
        } else {
            if (!$.isDecimal(data.amount)) {
                judge = false;
                amountDom.verifyError().verifyHelp(eContractDetail.message.basicInformation.amountInvalid);
            }
        }
    }
    return judge;
});

eContractDetail.verify.attachmentInformation.addEvent('attachmentSizeLimit', function() {
    var data = $('#form-detail').data('detail');
    var judge = true;
    var totalSize = 0;
    var attachmentList = [].concat(data.projectContractAttachmentList).concat(data.vendorRequestFormAttachmentList);
    for (var i = 0; i < attachmentList.length; i++)
        totalSize = $.mathJia(totalSize, $.toNumber(attachmentList.size));
    if (totalSize > 4 * 1024 * 1024) {
        judge = false
        _message += '\n' + eContractDetail.message.attachmentInformation.attachmentSizeLimit;
    };
    return judge;
});

eContractDetail.verify.addEvent('haveBuyApprover', function() {
    var formDetail = $('#form-detail').data('detail');
    var totalContractAmount = formDetail.totalContractAmount;
    if (totalContractAmount >= 100000) return true;
    return false;
});

eContractDetail.verify.addEvent('haveLegalApprover', function() {
    var formDetail = $('#form-detail').data('detail');
    var createPo = formDetail.createPo;
    var isMeeting = formDetail.isMeeting;
    var totalContractAmount = formDetail.totalContractAmount;
    if ((createPo && isMeeting && totalContractAmount >= 10000) || (createPo && !isMeeting && totalContractAmount >= 20000)) return true;
    return false;
});

eContractDetail.verify.addEvent('haveEcApprover', function() {
    var formDetail = $('#form-detail').data('detail');
    var acdd = formDetail.acdd;
    if (acdd) return true;
    return false;
});

eContractDetail.verify.addEvent('haveAtsApprover', function() {
    var formDetail = $('#form-detail').data('detail');
    var createPo = formDetail.createPo;
    var isMeeting = formDetail.isMeeting;
    var totalContractAmount = formDetail.totalContractAmount;
    judge = true;
    if (createPo) {
        if (isMeeting) {
            if (totalContractAmount > 0 && totalContractAmount < 10000) {
                judge = false;
            }
        } else {
            if (totalContractAmount > 0 && totalContractAmount < 20000) {
                judge = false;
            }
        }
    } else {
        if (isMeeting) {
            if (totalContractAmount >= 10000) {
                judge = false;
            }
        } else {
            if (totalContractAmount >= 20000) {
                judge = false;
            }
        }
    }
    //if ((!createPo) || (createPo && isMeeting && totalContractAmount >= 10000) || (!createPo && !isMeeting && totalContractAmount >= 20000)) return true;
    return judge;
});

eContractDetail.verify.addEvent('createPo', function() {
    var data = $('#form-detail').data('detail');
    var createPo = data.createPo;
    var isMeeting = data.isMeeting;
    var totalContractAmount = data.totalContractAmount;
    var judge = true;
    if (createPo) {
        if (isMeeting) {
            if (totalContractAmount > 0 && totalContractAmount < 10000) {
                judge = false;
                _message += '\n' + eContractDetail.message.mustNotcreatePo;
            }
        } else {
            if (totalContractAmount > 0 && totalContractAmount < 20000) {
                judge = false;
                _message += '\n' + eContractDetail.message.mustNotcreatePo;
            }
        }
    } else {
        if (isMeeting) {
            if (totalContractAmount >= 10000) {
                judge = false;
                _message += '\n' + eContractDetail.message.mustcreatePo;
            }
        } else {
            if (totalContractAmount >= 20000) {
                judge = false;
                _message += '\n' + eContractDetail.message.mustcreatePo;
            }
        }
    }
    return judge;
});

eContractDetail.verify.addEvent('status', function() {
    var data = _data.formDetail;
    var formDetail = data.formDetail;
    var formStatus = $.toNumber(data.formStatus);
    var action = $.getUrlParams(window.location.href, 'action');
    var backId = $.getUrlParams(window.location.href, 'backId');
    if (!eContractDetail.verify.isRequester() &&
        !eContractDetail.verify.isApprover() &&
        !eContractDetail.verify.isViewer()) {
        return $.alert(_enumMessage.forbidden, eContractDetail.form.exitForm);
    }
    $('#block-approval-information').addClass('dn');
    $('#block-curapproval-information').addClass('dn');
    $('#form-detail').disabledForm();
    $('#block-operation-information').enabledForm();
    $('#block-operation-information .btn[exit]').removeClass('dn');
    if ($.isEmpty(data.id)) {
        $('#form-detail').enabledForm();
        $('#block-operation-information').find('.btn[save], .btn[submit]').removeClass('dn');
        return false;
    }
    if(!$.isEmpty(action)){
        if(action == 'look') {
            switch (formStatus) {
                case _enumFormStatus.saved:
                    $('#block-operation-information').find().removeClass('dn');
                     break;
                case _enumFormStatus.submitted:
                    if (eContractDetail.verify.isApprover()) {
                        if(backId == 2) {
                            $('#block-curapproval-information').enabledForm();
                            $('#block-operation-information').find().removeClass('dn');
                        } else {
                            $('#block-curapproval-information').removeClass('dn');
                            $('#block-curapproval-information').enabledForm();
                            $('#block-operation-information').find('.btn[approve], .btn[reject]').removeClass('dn');
                        }
                    }
                    break;
                case _enumFormStatus.rejected1:
                      $('#block-approval-information').removeClass('dn');
                    break;
                case _enumFormStatus.rejected2:
                      $('#block-approval-information').removeClass('dn');
                    break;
                case _enumFormStatus.rejected3:
                        $('#block-approval-information').removeClass('dn');
                    break;
                case _enumFormStatus.rejected4:
                        $('#block-approval-information').removeClass('dn');
                    break;
                case _enumFormStatus.approved1:
                    $('#block-approval-information').removeClass('dn');
                    if(backId == 2) {
                        $('#block-curapproval-information').enabledForm();
                        $('#block-operation-information').find().removeClass('dn');
                    } else {
                        if (eContractDetail.verify.isApprover()) {
                            if (data.approvalStep == data.approvalFinalStep) {
                                $('#block-operation-information').find('.btn[approve]').removeClass('dn');
                            } else {
                                $('#block-curapproval-information').removeClass('dn');
                                $('#block-curapproval-information').enabledForm();
                                $('#block-operation-information').find('.btn[approve], .btn[reject]').removeClass('dn');
                            }
                        }
                    }
                    break;
                case _enumFormStatus.approved2:
                    $('#block-approval-information').removeClass('dn');
                    if(backId == 2) {
                        $('#block-curapproval-information').enabledForm();
                        $('#block-operation-information').find().removeClass('dn');
                    } else {
                        if (eContractDetail.verify.isApprover()) {
                            if (data.approvalStep == data.approvalFinalStep) {
                                $('#block-operation-information').find('.btn[approve]').removeClass('dn');
                            } else {
                                $('#block-curapproval-information').removeClass('dn');
                                $('#block-curapproval-information').enabledForm();
                                $('#block-operation-information').find('.btn[approve], .btn[reject]').removeClass('dn');
                            }
                        }
                    }
                    break;
                case _enumFormStatus.approved3:
                    $('#block-approval-information').removeClass('dn');
                    if(backId == 2) {
                        $('#block-curapproval-information').enabledForm();
                        $('#block-operation-information').find().removeClass('dn');
                    } else {
                        if (eContractDetail.verify.isApprover()) {
                            if (data.approvalStep == data.approvalFinalStep) {
                                $('#block-operation-information').find('.btn[approve]').removeClass('dn');
                            } else {
                                $('#block-curapproval-information').removeClass('dn');
                                $('#block-curapproval-information').enabledForm();
                                $('#block-operation-information').find('.btn[approve], .btn[reject]').removeClass('dn');
                            }
                        }
                    }
                    break;
                case _enumFormStatus.approved4:
                    $('#block-approval-information').removeClass('dn');
                    if(backId == 2) {
                        $('#block-curapproval-information').enabledForm();
                        $('#block-operation-information').find().removeClass('dn');
                    } else {
                        if (eContractDetail.verify.isApprover()) {
                            if (data.approvalStep == data.approvalFinalStep) {
                                $('#block-operation-information').find('.btn[approve]').removeClass('dn');
                            } else {
                                $('#block-curapproval-information').removeClass('dn');
                                $('#block-curapproval-information').enabledForm();
                                $('#block-operation-information').find('.btn[approve], .btn[reject]').removeClass('dn');
                            }
                        }
                    }
                    break;
                case _enumFormStatus.completed:
                    $('#block-approval-information').removeClass('dn');
                    break;
                default:
                    break;
            }
        }
        if(action == 'edit') {
            // $('#block-approval-information').removeClass('dn');
            if (eContractDetail.verify.isRequester()) {
                if(formStatus == _enumFormStatus.saved) {
                    console.log(formStatus == _enumFormStatus.saved)
                    $('#form-detail').enabledForm();
                    $('#block-operation-information').find('.btn[save], .btn[submit]').removeClass('dn');
                } else {
                    $('#form-detail').enabledForm();
                    $('#block-operation-information').find('.btn[save]').removeClass('dn');
                }
            }
        }
    }
});

eContractDetail.verify.addEvent('isRequester', function() {
    if ($.isEmpty(_data.formDetail.id)) return true;
    var data = _data.formDetail;
    if (_user.employeeId == data.sys_owner) return true;
    return false;
});

eContractDetail.verify.addEvent('isApprover', function() {
    if ($.isEmpty(_data.formDetail.id)) return false;
    if ($.isEmpty(_data.formDetail.approvalList)) return false;
    var data = _data.formDetail;
    var formDetail = data.formDetail;
    var approvalList = data.approvalList;
    var approvalStep = data.approvalStep;
    for (var i = 0; i < approvalList.length; i++)
        if (approvalList[i].approverEmployeeId == _user.employeeId && approvalList[i].approvalStep == approvalStep) return true;
    return false;
});

eContractDetail.verify.addEvent('isViewer', function() {
    if ($.isEmpty(_data.formDetail.id)) return false;
    // if ($.isEmpty(_data.formDetail.approvers)) return false;
    var data = $('#form-detail').data('detail');
    if (_user.employeeId == data.approvalStep1Approver) return true;
    if (_user.employeeId == data.approvalStep2Approver) return true;
    if (_user.employeeId == data.approvalStep3Approver) return true;
    if (_user.employeeId == data.approvalStep4Approver) return true;
    return false;
});

//message module
eContractDetail.message.basicInformation.addConfig('legalEntityRequired', '业务实体必填');
eContractDetail.message.basicInformation.addConfig('departmentRequired', '部门必填');
// eContractDetail.message.basicInformation.addConfig('isMeetingRequired', '是否是会议必填');
eContractDetail.message.basicInformation.addConfig('activityNameDescriptionRequired', '业务名称简述必填');
eContractDetail.message.basicInformation.addConfig('totalContractAmountRequired', '合同总金额必填');
eContractDetail.message.basicInformation.addConfig('totalContractAmountInvalid', '合同总金额格式不正确');
eContractDetail.message.basicInformation.addConfig('activityDateStartRequired', '预计的业务开展期间开始时间必填');
eContractDetail.message.basicInformation.addConfig('activityDateEndRequired', '预计的业务开展期间结束时间必填');
eContractDetail.message.basicInformation.addConfig('costCenterRequired', '成本中心必填');
eContractDetail.message.basicInformation.addConfig('amountRequired', '对应金额必填');
eContractDetail.message.basicInformation.addConfig('amountInvalid', '对应金额格式不正确');
eContractDetail.message.basicInformation.addConfig('costCenterAndAmountListLimit', '最多10个');
// eContractDetail.message.basicInformation.addConfig('createPoRequired', '是否要创建po必填');
// eContractDetail.message.basicInformation.addConfig('acddRequired', 'ACDD必填');

eContractDetail.message.vendorInformation.addConfig('vendorNameRequired', '合同方名称必填');

eContractDetail.message.attachmentInformation.addConfig('projectContractAttachmentListRequired', '项目合同必填');
eContractDetail.message.attachmentInformation.addConfig('vendorRequestFormAttachmentListRequired', '竞价分析报告必填');
eContractDetail.message.attachmentInformation.addConfig('attachmentSizeLimit', '所有附件大小不要超过4M');

eContractDetail.message.approverInformation.addConfig('buyApproverRequired', '采购审阅人必填');
eContractDetail.message.approverInformation.addConfig('legalApproverRequired', '法务审阅人必填');
eContractDetail.message.approverInformation.addConfig('ECApproverRequired', '合规审阅人必填');
eContractDetail.message.approverInformation.addConfig('atsApproverRequired', 'ATS审阅人必填');
eContractDetail.message.approverInformation.addConfig('stampApproverRequired', '公司印章持有人必填');

eContractDetail.message.addConfig('mustcreatePo', '当前情况一定要创建PO');
eContractDetail.message.addConfig('mustNotcreatePo', '当前情况不需要创建PO');
eContractDetail.message.addConfig('noCurPoCase', '当前情况不存在');

//data module